home *** CD-ROM | disk | FTP | other *** search
- package sub_arctic.output;
-
- import sub_arctic.lib.manager;
- import sub_arctic.lib.sub_arctic_error;
-
- import java.awt.Image;
- import java.awt.image.MemoryImageSource;
- import java.awt.image.FilteredImageSource;
- import java.awt.Component;
- import java.awt.Color;
- import java.awt.MediaTracker;
-
-
- /**
- * A class to encapsulate a potentially asynchronously loaded image. This
- * class does blocking for operations that need to wait until the image
- * is fully loaded (such as requests for the image's size).
- *
- * @author Scott Hudson
- */
- public class loaded_image {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Construct from an Image (this image is marked as not loaded yet).
- * @param Image img the Image to create the loaded_image from
- */
- public loaded_image(Image img)
- {
- _image = img;
- is_loaded = false;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Construct a blank in memory image of the given size and force it
- * to be "loaded" (not clear what that means for in-memory images, but
- * it seems to be required). The image is filled with the systems default
- * background color.
- *
- * @param int w the width of the image in pixels.
- * @param int h the height of the image in pixels.
- */
- public loaded_image(int w, int h)
- {
- drawable d;
- Color c;
- color_scheme cs=style_manager.default_color_scheme();
-
- /* create the empty image */
- _image = manager.an_awt_component().createImage(w,h);
-
- /* force a wait */
- /* ... except on Netscape 3.0 on the mac which hangs ... */
- if (manager.need_workaround(manager.WA_NS3_MAC, manager.WA_TRACKER_HANGS))
- is_loaded = true;
- else
- image();
-
- /* clear the base color so netscape 2.0x doesn't screw us */
- d=get_drawable();
- c=d.getColor();
- d.setColor(cs.base());
- d.fillRect(0,0,width(),height());
- d.setColor(c);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- *Construct from in memory data. This image is marked as unloaded.
- *
- * @param int[] the data to build the image from
- * @param int w the width of the image
- * @param int h the height of the image
- */
- public loaded_image(int[] data, int w, int h)
- {
- _image = manager.default_toolkit().createImage(
- new MemoryImageSource(w,h,data,0,w));
- is_loaded = false;
-
- /* ... unless of course we are on Netscape 3.0 on the mac ...*/
- if (manager.need_workaround(manager.WA_NS3_MAC, manager.WA_TRACKER_HANGS))
- is_loaded = true;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Do we know that the image has been loaded */
- protected boolean is_loaded = false;
-
- /** The image we encapsulate. */
- protected Image _image;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Get access to the encapsulated image without blocking if it is not
- * loaded yet.
- * @return Image the raw image (might not be loaded)
- */
- public Image raw_image() {return _image;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Get the image, but block if it is not completely loaded.
- * @return Image the encapsulated Image
- */
- public Image image()
- {
- boolean ok;
-
- /* if we know for sure that its loaded just return it. */
- if (is_loaded) return _image;
-
- /* use the manager utility routine to wait for it */
- ok = manager.wait_for_image(_image);
-
- /* if there was an error, substitute the "broken image" image */
- if (!ok || _image == null)
- {
- _image = manager.broken_image_icon().raw_image();
- }
-
- /* image is now loaded */
- is_loaded = true;
-
- return _image;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Get the image width, waiting for the image to load if necessary
- * @return int the width of the loaded_image in pixels
- */
- public int width()
- {
- int w;
-
- /* if the image is not currently reporting a width, wait for it */
- w = _image.getWidth(manager.an_observer());
- if (w < 0)
- {
- image();
- w = _image.getWidth(manager.an_observer());
- }
-
- /* if its still not reporting a width we have some sort of problem */
- if (w < 0) throw new sub_arctic_error("Improper width in loaded_image");
-
- return w;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Get the image height, waiting for the image to load if necessary
- * @return int the height of the loaded_image in pixels
- */
- public int height()
- {
- int h;
-
- /* if the image is not currently reporting a height, wait for it */
- h = _image.getHeight(manager.an_observer());
- if (h < 0)
- {
- image();
- h = _image.getHeight(manager.an_observer());
- }
-
- /* if its still not reporting a height we have some sort of problem */
- if (h < 0) throw new sub_arctic_error("Improper height in loaded_image");
-
- return h;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Get a drawable that can be used to draw on this image (similar to
- * getGraphics() on an AWT Image object).
- * @return drawable a context for drawing on this image
- */
-
- public drawable get_drawable()
- {
- /* build a drawable from the Graphics object we get from the raw image */
- return new drawable(raw_image().getGraphics());
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This function creates a nice looking loaded_image from an
- * an intensity image and a color. The intensity image is a black
- * and white image where the ti parameter (RGB (ti,ti,ti) means
- * transparent. <p>
- *
- * All shades of grey (other than the transparency intensity)
- * are mapped to an intensity in of the color passed, proportional
- * to their value (between 0 and 255).<p>
- *
- * Although these will get you "feathered" images, there is not
- * a lot of dynamic range before your chosen color falls off to
- * black. I would recommend making your intensity map have the
- * values 180+ to get reasonable results.<p>
- *
- * @param loaded_image map the intensity map.
- * @param Color color the target color.
- * @param int ti this the intensity which should be transparent.
- * @return loaded_image the result image.
- */
- public static loaded_image image_from_intensity_map(loaded_image map,
- Color color,
- int ti) {
-
- Image final_image,image;
- Component comp=manager.an_awt_component();
- MediaTracker mt=new MediaTracker(manager.an_awt_component());
-
- /* get the image ready */
- image=map.image();
-
- /* create the filter */
- mask_filter mf=new mask_filter(color,ti);
-
- /* process image with mask filter */
- final_image=
- comp.createImage(new FilteredImageSource(image.getSource(),mf));
- mt.addImage(final_image,0);
- try {
- mt.waitForID(0);
- } catch (InterruptedException e) {
- // very unexpected...
- manager.handle_unexpected_exception(e);
- }
-
- /* make a loaded image and return it */
- return new loaded_image(final_image);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
- }
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-